package com.aaa.jx.controller;

import com.aaa.jx.entity.SdsDetail;
import com.aaa.jx.entity.TSdsRecordDetail;
import com.aaa.jx.entity.TjMessage;
import com.aaa.jx.entity.Ttongji;
import com.aaa.jx.service.TSdsRecordDetailService;
import com.aaa.jx.service.impl.TSdsRecordDetailServiceImpl;

import com.alibaba.fastjson.JSONArray;
import com.util.LayData;
import com.util.PrintJsonData;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * (TSdsRecordDetail)表控制层
 *
 * @author makejava
 * @since 2023-05-08 14:38:55
 */


@WebServlet(name = "TSdsRecordDetailServlet", value = "/TSdsRecordDetailServlet")
public class TSdsRecordDetailServlet extends HttpServlet {

    private TSdsRecordDetailService tSdsRecordDetailService = new TSdsRecordDetailServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();

        TSdsRecordDetail recordDetail=new TSdsRecordDetail();
        try {
            BeanUtils.populate(recordDetail,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        // 
        String method = req.getParameter("method");
        if (method==null){


        }else if(method.equals("query")){
            String id = req.getParameter("id");
            int i = Integer.parseInt(id);
            List<SdsDetail> SdsDetails = tSdsRecordDetailService.queryById(i);
            LayData success=new LayData(0,"",0L,SdsDetails);
            PrintJsonData.printData(resp,success);
//            query(req,resp);
            return;
        }else if(method.equals("add")){
//            String data = req.getParameter("data");

            String[] dates = map.get("data");
            String date = dates[0];
            System.out.println(date);
            List<TSdsRecordDetail> list= JSONArray.parseArray(date,TSdsRecordDetail.class);
            System.out.println(list);
            for (TSdsRecordDetail tSdsRecordDetail : list) {
                tSdsRecordDetailService.insert(tSdsRecordDetail);
            }
            LayData success=new LayData(0,"",0L,null);
            PrintJsonData.printData(resp,success);
            //add(req,resp);
            return;
        }else if(method.equals("upd")){

           // upd(req,resp);
            return;
        }else if(method.equals("tj")){
            List<Ttongji> tjData = tSdsRecordDetailService.getTjData();
            LayData success=new LayData(0,"",0L,tjData);
            PrintJsonData.printData(resp,success);
            //del(req,resp);
            return;
        }else if(method.equals("tj2")){
            List<TjMessage> tjData2 = tSdsRecordDetailService.getTjData2();
            LayData success=new LayData(0,"",0L,tjData2);
            PrintJsonData.printData(resp,success);
            //del(req,resp);
            return;
        }

    }

}




